package org.example.channel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 写文件
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
        //创建文件输出流
        try (FileOutputStream fileOutputStream = new FileOutputStream("demo.txt")) {
            //获取文件通道
            FileChannel channel = fileOutputStream.getChannel();
            //创建缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            //写入缓冲区
            byteBuffer.put("666".getBytes());
            //翻转缓冲区
            byteBuffer.flip();
            //缓冲区写入通道
            int write = channel.write(byteBuffer);
            System.out.println(write);
        }
    }
}
